package com.suyuan.controller.admin;

import com.github.pagehelper.PageInfo;
import com.suyuan.entity.*;
import com.suyuan.service.EmployeeService;
import com.suyuan.service.PermissionEmployeeService;
import com.suyuan.service.PermissionService;
import com.suyuan.utils.JsonMessage;
import com.suyuan.web.Page;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 *
 * 企业员工控制类
 */
@Controller
@RequestMapping(value = "/admin/employee")
public class EmployeeController {

    @Resource(name = "employeeService")
    private EmployeeService employeeService;
    @Resource(name = "permissionService")
    private PermissionService permissionService;
    @Resource(name = "permissionEmployeeService")
    private PermissionEmployeeService peService;

    /**
     * 列表
     * @return
     */
    @RequestMapping(value = "/list")
    public String list(Integer employeeId, Integer compId, Integer depId, Page page, ModelMap model){
        PageInfo info = employeeService.list(employeeId,compId,depId,page);
        model.addAttribute("employeeId",employeeId);
        model.addAttribute("compId",compId);
        model.addAttribute("depId",depId);
        model.addAttribute("page",info);
        return "employee_list";
    }

    /**
     * 批量删除
     */
    @RequestMapping(value = "/deleteByIds")
    @ResponseBody
    public Object deleteByIds(String ids){
        if(StringUtils.isBlank(ids)){
            return JsonMessage.error("删除失败");
        }
        String[] idArr = null;
        if(ids.length() >= 2){
            idArr = ids.split(",");
        }else{
            idArr = new String[]{ids};
        }
        for(String id : idArr){
            employeeService.delete(Integer.parseInt(id));
        }
        return JsonMessage.success("删除成功");
    }

    /**
     * 编辑
     */
    @RequestMapping(value = "/edit")
    public String edit(Integer id,ModelMap model){
        //此处无须分页。目的是获取全部权限集合
        PageInfo pageInfo = permissionService.list(new Page(1, 1000));
        List<Permission> permissions = pageInfo.getList();

        //获取员工也配置权限列表
        List<PermissionEmployee> peList = peService.findByEmpId(id);
        model.addAttribute("peList",peList);

        EmployeeInfo employee = employeeService.findById(id);
        List<CompanyInfo> companys = employeeService.getCompanys();
        List<DepartmentInfo> depts = employeeService.getDepts();

        model.addAttribute("employee",employee);
        model.addAttribute("companys",companys);
        model.addAttribute("depts",depts);
        model.addAttribute("permissions",permissions);
        return "employee_edit";
    }

    /**
     * 更新
     * @return
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(EmployeeInfo employee,@RequestParam("perIds") Integer[] perIds){
        System.out.println("employee:"+employee);
        for(Integer pid : perIds){
            System.out.println("perid:"+pid);
        }
        //先将该员工之前已添加的全部权限清空，后更新新的权限信息
        peService.deleteByEmpId(employee.getGid());

        //员工编辑处允许更改相关用户权限
        int code  = employeeService.update(employee);
        //新建权限_员工对象
        for(Integer pid : perIds){
            PermissionEmployee pe = new PermissionEmployee();
            pe.setEid(employee.getGid());
            pe.setPid(pid);
            peService.insertSelective(pe);
        }

        if(code == 1){
            return JsonMessage.success("更新成功");
        }
        return JsonMessage.error("更新失败");

    }


    /**
     * 添加页面
     * @return
     */
    @RequestMapping(value = "/add")
    public String add(ModelMap model){
        List<CompanyInfo> companys = employeeService.getCompanys();
        List<DepartmentInfo> depts = employeeService.getDepts();
        model.addAttribute("companys",companys);
        model.addAttribute("depts",depts);
        return "employee_add";
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(Integer id){
        employeeService.delete(id);
        return JsonMessage.success("删除成功");
    }

    /**
     * 保存
     * @param employee
     * @return
     */
    @RequestMapping( value = "/save")
    @ResponseBody
    public Object save(EmployeeInfo employee){
        SimpleDateFormat sp = new SimpleDateFormat("yyyyMMddhhmmss");
        //设置入职时间
        employee.setEmplWorkTime(new Date());
        //员工编号
        employee.setEmplWorkId(sp.format(new Date()));
        //员工识别码
        employee.setEmplCode(UUID.randomUUID().toString());

        int code = employeeService.save(employee);
        if(code == 1){
            return JsonMessage.success("添加成功");
        }
        return JsonMessage.error("添加失败");
    }
}
